<template>
  <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" append-to-body>
    <el-table :data="tableData" row-key="id" border stripe @row-click="singleElection"
              default-expand-all :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="区域名称" width="240" align="left">
        <template slot-scope="scope">
          <el-radio class="radio" v-model="templateSelection" :label="scope.row.areaName">
            {{
              scope.row.areaName
            }}
          </el-radio>
        </template>
      </el-table-column>
      <!--      <el-table-column label="区域名称" align="left" prop="areaName"/>-->
      <el-table-column label="区域类型" align="center" prop="levelType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.level_type" :value="scope.row.levelType"/>
        </template>
      </el-table-column>
      <el-table-column label="排序号" align="center" prop="rank"/>
      <el-table-column label="区域描述" align="center" prop="details"/>
      <el-table-column label="备注" align="center" prop="remark"/>
    </el-table>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="chooseAndReturn">选择并返回</el-button>
      <el-button type="danger" @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {listZone} from '@/api/leakage/zone'

export default {
  name: "RegionSingleSelector",
  props: {
    callback: {
      type: Function,
      default: (data) => {

      }
    }
  },
  dicts: ['level_type'],
  data() {
    return {
      open: false,
      title: '目标对象选择',
      queryParams: {},
      templateSelection: '',
      currentSelectedRow: undefined,
      // 返回数据
      tableData: [],
      targetTypeObj: undefined
    }
  },
  methods: {
    openDialog(targetType) {
      this.targetTypeObj = targetType
      this.open = true
      this.loadRegionTree()
    },
    chooseAndReturn() {
      if (this.currentSelectedRow == undefined) {
        this.$message.error('请先选择目标对象')
      } else if (this.currentSelectedRow.levelType != this.targetTypeObj) {
        this.$message.error('您选择的目标对象不符合要求，请重新选择')
      } else {
        this.callback(this.currentSelectedRow)
        this.cancel()
      }
    },
    cancel() {
      this.currentSelectedRow = undefined
      this.open = false
    },
    singleElection(row) {
      this.templateSelection = row.id
      this.currentSelectedRow = row
    },
    /** 查询区域管理列表 */
    loadRegionTree() {
      listZone(this.queryParams).then(res => {
        this.tableData = res
      })
    },
  }
}
</script>

<style scoped>

</style>
